package com.gdut.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdut.commonutils.JwtUtils;
import com.gdut.commonutils.R;
import com.gdut.commonutils.ordervo.UcenterMemberOrder;
import com.gdut.eduservice.client.UcenterClient;
import com.gdut.eduservice.entity.EduComment;
import com.gdut.eduservice.service.EduCommentService;
import com.gdut.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author Drifiter
 * @create 2021-09-02 13:29
 */
@RequestMapping("eduservice/commentfront")
@RestController
//@CrossOrigin
public class CommentFrontController {


    @Autowired
    private UcenterClient ucenterClient;
    //根据课程id查询评论列表

    @Autowired
    private EduCommentService commentService;

    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
//            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable long page,
//            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable long limit,
            String courseId){

        Page<EduComment> pageParam = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        wrapper.orderByDesc("gmt_create");
        commentService.page(pageParam,wrapper);

        List<EduComment> commentList = pageParam.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("items",commentList);
        map.put("current",pageParam.getCurrent());
        map.put("records",pageParam.getRecords());
        map.put("total",pageParam.getTotal());
        map.put("pages",pageParam.getPages());
        map.put("size",pageParam.getSize());
        map.put("hasNest",pageParam.hasNext());
        map.put("hasPrevious",pageParam.hasPrevious());

        return R.ok().data(map);

    }


    //添加评论
    @PostMapping("/auth/addComment")
    public R addComment(HttpServletRequest request,@RequestBody EduComment eduComment){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"请先登录");
        }
        eduComment.setMemberId(memberId);

        //远程调用ucenter根据用户id获取用户信息
        UcenterMemberOrder userInfo = ucenterClient.getUserInfo(memberId);

        eduComment.setAvatar(userInfo.getAvatar());
        eduComment.setNickname(userInfo.getNickname());

        //保存评论
        commentService.save(eduComment);

        return R.ok();
    }

    

}
